package com.bkck.sharkblockade.instructions;

import com.bkck.sharkblockade.menu.R;
import com.bkck.sharkblockade.menu.Menu;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

/**
 * The Highscores page.
 * 
 * @author Team BKCK.
 * 
 */
public class Instructions extends Activity {

  ScrollView scroller;
  TextView page;
  TextView title;
  TextView content;
  TableLayout table;
  TextView previous;
  TextView next;

  int pageInt = 1;

  public static final String TAG = Instructions.class.getSimpleName();

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // requesting to turn the title OFF
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    // making it full screen
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.instructions);

    table = new TableLayout(this);
    scroller = (ScrollView) findViewById(R.id.scroller);
    title = new TextView(this);
    title.setText("Overview");
    title.setGravity(Gravity.CENTER_HORIZONTAL);
    title.setTextSize(20);
    content = new TextView(this);
    content.setGravity(Gravity.CENTER_HORIZONTAL);
    String text = "\tThis is a Breakout clone.";
    text += "To play, select \"Play\" from the main menu.";
    text += "Then, select the level that you want to play.";
    text += "To move the paddle, touch below the paddle and move ";
    text += "your finger left and right accordingly.";
    text += "(If playing on an emulator, click on the paddle ";
    text += "and drag the paddle left and right.)";
    content.setText(text);
    content.setTextSize(14);
    table.addView(title);
    table.addView(content);
    scroller.addView(table);
    page = (TextView) findViewById(R.id.page);
    previous = (TextView) findViewById(R.id.previous);
    next = (TextView) findViewById(R.id.next);
  }

  /**
   * Switch between instructions.
   * 
   * @param view
   */
  public void goBack(View view) {
    scroller.removeAllViews();
    table.removeAllViews();
    pageInt -= 1;
    if (pageInt < 1) {
      pageInt = 5;
    }

    TextView text1 = new TextView(this);
    ImageView image1 = new ImageView(this);
    TableRow row1 = new TableRow(this);
    TextView text2 = new TextView(this);
    ImageView image2 = new ImageView(this);
    TableRow row2 = new TableRow(this);
    TextView text3 = new TextView(this);
    ImageView image3 = new ImageView(this);
    TableRow row3 = new TableRow(this);
    TextView text4 = new TextView(this);
    ImageView image4 = new ImageView(this);
    TableRow row4 = new TableRow(this);
    TextView text5 = new TextView(this);
    ImageView image5 = new ImageView(this);
    TableRow row5 = new TableRow(this);
    TextView text6 = new TextView(this);
    ImageView image6 = new ImageView(this);
    TableRow row6 = new TableRow(this);
    TextView text7 = new TextView(this);
    ImageView image7 = new ImageView(this);
    TableRow row7 = new TableRow(this);
    TextView text8 = new TextView(this);
    ImageView image8 = new ImageView(this);
    TableRow row8 = new TableRow(this);
    TextView text9 = new TextView(this);
    ImageView image9 = new ImageView(this);
    TableRow row9 = new TableRow(this);
    
    row1.setGravity(Gravity.CENTER_HORIZONTAL);
    row2.setGravity(Gravity.CENTER_HORIZONTAL);
    row3.setGravity(Gravity.CENTER_HORIZONTAL);
    row4.setGravity(Gravity.CENTER_HORIZONTAL);
    row5.setGravity(Gravity.CENTER_HORIZONTAL);
    row6.setGravity(Gravity.CENTER_HORIZONTAL);
    row7.setGravity(Gravity.CENTER_HORIZONTAL);
    row8.setGravity(Gravity.CENTER_HORIZONTAL);
    row9.setGravity(Gravity.CENTER_HORIZONTAL);

    
    switch (pageInt) {
    // Overview
    case 1:
      page.setText("1/5");
      scroller = (ScrollView) findViewById(R.id.scroller);
      title.setText("Overview");
      String text = "\tThis is a Breakout clone.";
      text += "To play, select \"Play\" from the main menu.  ";
      text += "Then, select the level that you want to play.  ";
      text += "To move the paddle, touch below the paddle and move ";
      text += "your finger left and right accordingly.  ";
      text += "(If playing on an emulator, click on the paddle ";
      text += "and drag the paddle left and right.)";
      content.setText(text);
      content.setTextSize(14);
      table.addView(title);
      table.addView(content);
      previous.setText("Other");
      next.setText("Blocks");
      break;
    // Blocks
    case 2:
      page.setText("2/5");
      title.setText("Blocks");
      table.addView(title);
      content.setText("The goal of the game is to destroy all colored blocks and bomb blocks."
          + "\n\nBelow is a description of each type of block:\n");
      table.addView(content);
      image1.setBackgroundResource(R.drawable.redblock);
      text1.setText("\tTakes three hits to destroy.");
      text1.setTextSize(14);
      row1.addView(image1);
      row1.addView(text1);
      table.addView(row1);
      image2.setBackgroundResource(R.drawable.yellowblock);
      text2.setText("\tTakes two hits to destroy.");
      row2.addView(image2);
      row2.addView(text2);
      table.addView(row2);
      image3.setBackgroundResource(R.drawable.greenblock);
      text3.setText("\tTakes one hit to destroy.");
      row3.addView(image3);
      row3.addView(text3);
      table.addView(row3);
      image4.setBackgroundResource(R.drawable.bombblock);
      text4.setText("\tDestroys adjacent blocks.");
      row4.addView(image4);
      row4.addView(text4);
      table.addView(row4);
      image5.setBackgroundResource(R.drawable.grayblock);
      text5.setText("\tIndestructible");
      row5.addView(image5);
      row5.addView(text5);
      table.addView(row5);
      previous.setText("Overview");
      next.setText("Powerups");
      break;
    // Powerups
    case 3:
      page.setText("3/5");
      title.setText("Powerups");
      table.addView(title);
      content.setText("Powerups can either help you or hinder you."
          + "\n\nBelow is a description of each type of powerup:\n");
      table.addView(content);
      image1.setBackgroundResource(R.drawable.paddle_increase);
      text1.setText("\tMakes the paddle larger.");
      text1.setTextSize(14);
      row1.addView(image1);
      row1.addView(text1);
      table.addView(row1);
      image2.setBackgroundResource(R.drawable.paddle_decrease);
      text2.setText("\tMakes the paddle smaller.");
      row2.addView(image2);
      row2.addView(text2);
      table.addView(row2);
      image3.setBackgroundResource(R.drawable.red_clock);
      text3.setText("\tMakes the ball faster.");
      row3.addView(image3);
      row3.addView(text3);
      table.addView(row3);
      image4.setBackgroundResource(R.drawable.green_clock);
      text4.setText("\tMakes the ball slower.");
      row4.addView(image4);
      row4.addView(text4);
      table.addView(row4);
      image5.setBackgroundResource(R.drawable.orange_clock);
      text5.setText("\tMakes the shark(s) faster.");
      row5.addView(image5);
      row5.addView(text5);
      table.addView(row5);
      image6.setBackgroundResource(R.drawable.blue_clock);
      text6.setText("\tMakes the shark(s) slower.");
      row6.addView(image6);
      row6.addView(text6);
      table.addView(row6);
      image7.setBackgroundResource(R.drawable.harpoon_item);
      text7.setText("\tTouch screen to launch.");
      row7.addView(image7);
      row7.addView(text7);
      table.addView(row7);
      image8.setBackgroundResource(R.drawable.spiked);
      text8.setText("\tBreak through multiple blocks.");
      row8.addView(image8);
      row8.addView(text8);
      table.addView(row8);
      image9.setBackgroundResource(R.drawable.ghost);
      text9.setText("\tPasses through objects.");
      row9.addView(image9);
      row9.addView(text9);
      table.addView(row9);
      previous.setText("Blocks");
      next.setText("Sharks");
      break;
    // Sharks
    case 4:
      page.setText("4/5");
      title.setText("Sharks");
      table.addView(title);
      content.setText("Sharks are the main obstacle of the game.  If your ball comes into"
              + " contact with a shark it will be eaten.\n\n"
              + "Sharks can only be killed with the harpoon powerup; however, another will come"
              + " to take its place after awhile.\n\n");
      table.addView(content);
      image1.setBackgroundResource(R.drawable.orange_shark_right);
      row1.addView(image1);
      row1.setPadding(0, 0, 0, 20);
      image2.setBackgroundResource(R.drawable.blue_shark_left);
      row2.addView(image2);
      table.addView(row1);
      table.addView(row2);
      previous.setText("Powerups");
      next.setText("Other");
      break;
      // Other Obstacles
    case 5:
      page.setText("5/5");
      title.setText("Other Obstacles");
      table.addView(title);
      content.setText("There are also several creatures " + 
    		  "that will attempt to impede your play.\n\n");
      table.addView(content);
      text1.setText("Jellyfish will fall from the top of the screen.\n" +
    		  "If a jellyfish comes in contact with the paddle, it will " +
    		  "stun the paddle, stopping it from moving.\n\n");
      text1.setHorizontallyScrolling(false);
      text1.setMaxWidth(256);
      text1.setGravity(Gravity.CENTER);
      row1.addView(text1);
      image1.setScaleType(ImageView.ScaleType.CENTER);
      image1.setBackgroundResource(R.drawable.jellyfish);
      image1.setMaxWidth(32);
      image1.setMaxHeight(40);
      row1.addView(image1);
//      row2.setPadding(0, 0, 0, 20);
      text2.setText("Fish will deflect the ball if the ball hits them.\n" +
    		  "They may be considered mobile indestructible blocks.\n\n");
      text2.setHorizontallyScrolling(false);
      text2.setMaxWidth(256);
      text2.setGravity(Gravity.CENTER);
      row2.addView(text2);
      image2.setScaleType(ImageView.ScaleType.CENTER);
      image2.setBackgroundResource(R.drawable.fish);
      image2.setMaxWidth(32);
      image2.setMaxHeight(15);
      row2.addView(image2);
//      row4.setPadding(0, 0, 0, 20);
      table.addView(row1);
      table.addView(row2);
//      table.addView(row3);
//      table.addView(row4);
      previous.setText("Sharks");
      next.setText("Overview");
      break;
      
    default:

    }
    scroller.addView(table);
  }

  /**
   * Switch between instructions.
   * 
   * @param view
   */
  public void goForward(View view) {
    scroller.removeAllViews();
    table.removeAllViews();
    pageInt += 1;
    if (pageInt > 5) {
      pageInt = 1;
    }

    TextView text1 = new TextView(this);
    ImageView image1 = new ImageView(this);
    TableRow row1 = new TableRow(this);
    TextView text2 = new TextView(this);
    ImageView image2 = new ImageView(this);
    TableRow row2 = new TableRow(this);
    TextView text3 = new TextView(this);
    ImageView image3 = new ImageView(this);
    TableRow row3 = new TableRow(this);
    TextView text4 = new TextView(this);
    ImageView image4 = new ImageView(this);
    TableRow row4 = new TableRow(this);
    TextView text5 = new TextView(this);
    ImageView image5 = new ImageView(this);
    TableRow row5 = new TableRow(this);
    TextView text6 = new TextView(this);
    ImageView image6 = new ImageView(this);
    TableRow row6 = new TableRow(this);
    TextView text7 = new TextView(this);
    ImageView image7 = new ImageView(this);
    TableRow row7 = new TableRow(this);
    TextView text8 = new TextView(this);
    ImageView image8 = new ImageView(this);
    TableRow row8 = new TableRow(this);
    TextView text9 = new TextView(this);
    ImageView image9 = new ImageView(this);
    TableRow row9 = new TableRow(this);
    
    row1.setGravity(Gravity.CENTER_HORIZONTAL);
    row2.setGravity(Gravity.CENTER_HORIZONTAL);
    row3.setGravity(Gravity.CENTER_HORIZONTAL);
    row4.setGravity(Gravity.CENTER_HORIZONTAL);
    row5.setGravity(Gravity.CENTER_HORIZONTAL);
    row6.setGravity(Gravity.CENTER_HORIZONTAL);
    row7.setGravity(Gravity.CENTER_HORIZONTAL);
    row8.setGravity(Gravity.CENTER_HORIZONTAL);
    row9.setGravity(Gravity.CENTER_HORIZONTAL);
    
    switch (pageInt) {
    // Overview
    case 1:
      page.setText("1/5");
      scroller = (ScrollView) findViewById(R.id.scroller);
      title.setText("Overview");
      String overviewText = "\tThis is a Breakout clone.";
      overviewText += "To play, select \"Play\" from the main menu.  ";
      overviewText += "Then, select the level that you want to play.  ";
      overviewText += "To move the paddle, touch below the paddle and move ";
      overviewText += "your finger left and right accordingly.  ";
      overviewText += "(If playing on an emulator, click on the paddle ";
      overviewText += "and drag the paddle left and right.)";
      content.setText(overviewText);
      content.setTextSize(14);
      table.addView(title);
      table.addView(content);
      previous.setText("Other");
      next.setText("Blocks");
      break;
    // Blocks
    case 2:
      page.setText("2/5");
      title.setText("Blocks");
      table.addView(title);
      content.setText("The goal of the game is to destroy all colored blocks and bomb blocks."
          + "\n\nBelow is a description of each type of block:\n");
      table.addView(content);
      image1.setBackgroundResource(R.drawable.redblock);
      text1.setText("\tTakes three hits to destroy.");
      text1.setTextSize(14);
      row1.addView(image1);
      row1.addView(text1);
      table.addView(row1);
      image2.setBackgroundResource(R.drawable.yellowblock);
      text2.setText("\tTakes two hits to destroy.");
      row2.addView(image2);
      row2.addView(text2);
      table.addView(row2);
      image3.setBackgroundResource(R.drawable.greenblock);
      text3.setText("\tTakes one hit to destroy.");
      row3.addView(image3);
      row3.addView(text3);
      table.addView(row3);
      image4.setBackgroundResource(R.drawable.bombblock);
      text4.setText("\tDestroys adjacent blocks.");
      row4.addView(image4);
      row4.addView(text4);
      table.addView(row4);
      image5.setBackgroundResource(R.drawable.grayblock);
      text5.setText("\tIndestructible");
      row5.addView(image5);
      row5.addView(text5);
      table.addView(row5);
      previous.setText("Overview");
      next.setText("Powerups");
      break;
    // Powerups
    case 3:
      page.setText("3/5");
      title.setText("Powerups");
      table.addView(title);
      content.setText("Powerups can either help you or hinder you."
          + "\n\nBelow is a description of each type of powerup:\n");
      table.addView(content);
      image1.setBackgroundResource(R.drawable.paddle_increase);
      text1.setText("\tMakes the paddle larger.");
      text1.setTextSize(14);
      row1.addView(image1);
      row1.addView(text1);
      table.addView(row1);
      image2.setBackgroundResource(R.drawable.paddle_decrease);
      text2.setText("\tMakes the paddle smaller.");
      row2.addView(image2);
      row2.addView(text2);
      table.addView(row2);
      image3.setBackgroundResource(R.drawable.red_clock);
      text3.setText("\tMakes the ball faster.");
      row3.addView(image3);
      row3.addView(text3);
      table.addView(row3);
      image4.setBackgroundResource(R.drawable.green_clock);
      text4.setText("\tMakes the ball slower.");
      row4.addView(image4);
      row4.addView(text4);
      table.addView(row4);
      image5.setBackgroundResource(R.drawable.orange_clock);
      text5.setText("\tMakes the shark(s) faster.");
      row5.addView(image5);
      row5.addView(text5);
      table.addView(row5);
      image6.setBackgroundResource(R.drawable.blue_clock);
      text6.setText("\tMakes the shark(s) slower.");
      row6.addView(image6);
      row6.addView(text6);
      table.addView(row6);
      image7.setBackgroundResource(R.drawable.harpoon_item);
      text7.setText("\tTouch screen to launch.");
      row7.addView(image7);
      row7.addView(text7);
      table.addView(row7);
      image8.setBackgroundResource(R.drawable.spiked);
      text8.setText("\tBreak through multiple blocks.");
      row8.addView(image8);
      row8.addView(text8);
      table.addView(row8);
      image9.setBackgroundResource(R.drawable.ghost);
      text9.setText("\tPasses through objects.");
      row9.addView(image9);
      row9.addView(text9);
      table.addView(row9);
      previous.setText("Blocks");
      next.setText("Sharks");
      break;
    // Sharks
    case 4:
      page.setText("4/5");
      title.setText("Sharks");
      table.addView(title);
      content.setText("Sharks are the main obstacle of the game.  If your ball comes into"
          + " contact with a shark it will be eaten.\n\n"
          + "Sharks can only be killed with the harpoon powerup; however, another will come"
          + " to take its place after awhile.\n\n");
      table.addView(content);
      image1.setBackgroundResource(R.drawable.orange_shark_right);
      row1.addView(image1);
      row1.setPadding(0, 0, 0, 20);
      image2.setBackgroundResource(R.drawable.blue_shark_left);
      row2.addView(image2);
      table.addView(row1);
      table.addView(row2);
      previous.setText("Powerups");
      next.setText("Other");
      break;
      // Other Obstacles
    case 5:
      page.setText("5/5");
      title.setText("Other Obstacles");
      table.addView(title);
      content.setText("There are also several creatures " + 
    		  "that will attempt to impede your play.\n\n");
      table.addView(content);
      text1.setText("Jellyfish will fall from the top of the screen.\n" +
    		  "If a jellyfish comes in contact with the paddle, it will " +
    		  "stun the paddle, stopping it from moving.\n\n");
      text1.setHorizontallyScrolling(false);
      text1.setMaxWidth(256);
      text1.setGravity(Gravity.CENTER);
      row1.addView(text1);
      image1.setScaleType(ImageView.ScaleType.CENTER);
      image1.setBackgroundResource(R.drawable.jellyfish);
      image1.setMaxWidth(32);
      image1.setMaxHeight(40);
      row1.addView(image1);
//      row2.setPadding(0, 0, 0, 20);
      text2.setText("Fish will deflect the ball if the ball hits them.\n" +
    		  "They may be considered mobile indestructible blocks.\n\n");
      text2.setHorizontallyScrolling(false);
      text2.setMaxWidth(256);
      text2.setGravity(Gravity.CENTER);
      row2.addView(text2);
      image2.setScaleType(ImageView.ScaleType.CENTER);
      image2.setBackgroundResource(R.drawable.fish);
      image2.setMaxWidth(32);
      image2.setMaxHeight(15);
      row2.addView(image2);
//      row4.setPadding(0, 0, 0, 20);
      table.addView(row1);
      table.addView(row2);
//      table.addView(row3);
//      table.addView(row4);
      previous.setText("Sharks");
      next.setText("Overview");
      break;
      
    default:

    }
    scroller.addView(table);
  }

  /**
   * Switch to the activity for menu.
   * 
   * @param view The view.
   */
  public void toMenu(View view) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setClassName(this, Menu.class.getName());
    startActivity(intent);
  }

  @Override
  protected void onStop() {
    Log.d(TAG, "instruction onStop");
    finish();
    super.onDestroy();
  }

  /**
   * Ignore screen orientation change.
   */
  @Override
  public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
  }
}